<?php

/**
 * Identifies translations disputes 
 *
 * @package BABEL
 * @since   BABEL 0.1.0
 * @version $Id: view_disputes.php 766 2008-05-14 08:52:59Z bjori $
 * @copyright Copyright (C) 2008 Hannes Magnusson
 * @copyright Copyright (C) 2008 Frank Bølviken
 * @author  Hannes Magnusson
 * @author  Frank Bølviken
 * @license LGPL
 */  

$action = isset($_POST["action"]) ? $_POST["action"] : "all_disputes";


/*
 * Checks if user has pushed VIEW on a dispute.
 * If the user has, also checks if the CONST_ID isnt empty.
 * If this is true, $action is set to "single_dispute"
 */
if(isset($_POST["btn_view_single"])) {
	if($_POST["CONST_ID"] != "") {
		$action = "single_dispute";
	}
	else
		$obj_tpl->setVar("VAR_NO_DISPUTES", $obj_html->displayMessage("error", "In order to view any disputes you have to choose a language."));
}

/*
 * Checks if the user has pushed the submit button in single_despute section
 * 
 * Inserts the forced translation which the admin has chosen.
 */
if(isset($_POST["btn_sub"])) {
	
	/* 
	 * SQL statement which inserts a new translation, with status FINAL.
	 * This is a forced translation by an admin
	 */
	$q = "
		INSERT INTO
			babel_translations (
				status, 
				created, 
				changed_date, 
				enabled_date, 
				user_id, 
				translated_string, 
				language_id, 
				value_id
			) VALUES(
				?,
				NOW(),
				NOW(),
				NOW(),
				?,
				?,
				?,
				?)
		";
	// Runs the query, sets $failed = TRUE if there was any errors.
	$failed = !PDODB::execQuery($q, array(
										BABEL_DISPUTE_FORCE,
										$_SESSION["user"]["user_id"],
										$_POST["admin_suggestion"],
										$_POST["langid"],
										$_POST["valueid"]
										)
						);
	if ($failed) {
		throw new Exception("Wierd return value");
	}

	// Returns the user to "View all disputes" section.
	$action = "all_disputes";
}

/*
 * To differents states of the view_dispute page. 
 *
 * View all of the disputes 	("all_disputes")
 * View a single disput 		("single_dispute")
 */

switch($action) {
	case "all_disputes" : 
		$obj_tpl->setFile("babel_disputes", "babel/view_disputes.html");
		$obj_tpl->setBlock("babel_disputes", "list_language", "language_ref");
		
		// Get the language data from the database
		$failed = !PDODB::runQuery("
				  					SELECT
				 						language_id,
				   						name
				  					FROM 
			      						babel_languages
			  						", null, $stmt);

		if ($failed) {
			throw new Exception("There was something wrong with the language query");
		}
		
		// Looping trough the languages. Printing them out in the template.
		foreach($stmt as $row) {
		$obj_tpl->setVar(array(
							   "LANG_ID"	=>	$row["language_id"],
							   "LANG_NAME"	=> 	$row["name"]
							  )
						);
		$obj_tpl->parse("language_ref", "list_language", true);
		}
		
		/*
		 * User has chosen a language, and pushed the VIEW button.
		 *
		 * All the disputes for that language is fetched, and printed out.
		 */
		if(isset($_POST["view_disp"])) {
			$langid = $_POST["language_select"];
		
			// User hasn't picked a language in the list, and therefor $langid 
			// is the default -1 value.	
			if($langid == -1) {
				$obj_tpl->setVar("VAR_NO_DISPUTES", $obj_html->displayMessage("error", "Please choose a language!"));
			}
			else {

				/*
				 * SQL statement which returns all of the disputes for a given 
				 * language, based on the following values:
				 *
				 * $langid 							The language id
				 * BABEL_DISPUTE_FORCE  			Constant defining status FINAL
				 * BABEL_TRANSLATION_TRANSLATION 	Constant defining status TRANSLATION stage
				 * BABEL_DISPUTE_LIMIT 				Constant defining dispute limit
				 */
				$DB_QUERY = "
					SELECT
						t.value_id,
						COUNT(t.value_id) as number,
						v.original_string,
						t.language_id
					FROM
						babel_translations t, babel_values v
					WHERE
						t.language_id = ?
					AND
						t.value_id = v.value_id
					AND 
						t.value_id NOT IN (SELECT value_id FROM babel_translations WHERE status=? AND language_id = ?)
					AND
						t.status IN (?,?)
					GROUP BY 
						t.value_id
					HAVING
						number > ?
					";
				
				// Runs the query, and sets $failed = TRUE if there was any 
				// errors
				$failed = !PDODB::runQuery($DB_QUERY, array($langid, BABEL_DISPUTE_FORCE, $langid, BABEL_TRANSLATE_STATUS_VOTE, BABEL_TRANSLATE_STATUS_REJECTED,  BABEL_DISPUTE_LIMIT), $stmt);
				if ($failed || ($rowCount = $stmt->rowCount()) == 0) {
					if($failed) {
						throw new Exception("There was something wrong with the query");
					}
					else
						$obj_tpl->setVar("VAR_NO_DISPUTES", $obj_html->displayMessage("info", "No disputes for this language!"));
				}

				/*
				 * Looping through the $stmt array, and prints them out in the 
				 * template.
				 */
				else {	
					$obj_tpl->setBlock("babel_disputes", "list_disputes", "list_ref");
				
					$rowCount = 1;
					foreach($stmt as $row) {
						$obj_tpl->setVar(array(
											"CONST_ID"			=> $row["value_id"],
											"NUM_TRANSLATIONS"	=> $row["number"],
											"TRANSLATION_TEXT"	=> $row["original_string"],
											"LANG_ID"			=> $row["language_id"],
						
											"ROW_TR_BGCOLOR"		=> $rowCount++ % 2 ? ROW_COLOR_1 : ROW_COLOR_2
											)
										);
						$obj_tpl->parse("list_ref", "list_disputes", true);
					}
				}	
			}
		}
		
		$obj_tpl->pparse("babel_disputes", "babel_disputes");
	break;

	case "single_dispute" :
		
		$obj_tpl->setFile("babel_single_dispute", "babel/view_single_dispute.html");
	
		/*
		 * Checks if the following values is set, if it is, continues to process 
		 * the information.
		 */ 	
		if (isset($_POST["LANG_ID"]) && isset($_POST["CONST_ID"])) {
			$valueid = $_POST["CONST_ID"];
			$langid = $_POST["LANG_ID"];
			
			/* 
			 * SQL statement which returns a dispute, based on the following 
			 * values:
			 *
			 * $valueid 	The value of the dispute
			 * $langid 		The language of the dispute
			 */
			$DB_QUERY = "
				SELECT
					t.value_id,
					t.language_id,
					t.user_id,
					t.changed_date,
					t.translated_string,
					t.vote_points,
					v.original_string
				FROM
					babel_translations t, babel_values v
				WHERE
					t.value_id = ?
				AND 
					t.language_id = ?
				AND 
					t.value_id = v.value_id
				ORDER BY 
					t.changed_date DESC
			";	

			// Runs the query, and sets $failed = TRUE if there was any error	
			$failed = !PDODB::runQuery($DB_QUERY, array($valueid,$langid), $stmt);

			if ($failed || ($rowCount = $stmt->rowCount()) == 0) {
				throw new Exception("There was something wrong with the query");
			}
			else{
				$obj_tpl->setBlock("babel_single_dispute", "changes", "changes_ref");
				
				/*
					* Loops through the $stmt array which contains the dispute 
					* information, and prints it out in the template
					*/ 
				$rowCount = 1;
				foreach($stmt as $row) {
					if($rowCount == 1) {
						
						// Prints the general dispute data, and the newest 
						// translation.
						$obj_tpl->setVar(array(
												"VAR_LANGID"			=> $row["language_id"],
												"VAR_VALUEID"		=> $row["value_id"],
												"LATEST_SUGGESTION"	=> $row["translated_string"],
												"ORIGINAL_TEXT"		=> $row["original_string"]
												)
										);
					}
					// Prints the changelog
					$obj_tpl->setVar(array(
											"NR" 				=> $rowCount,
											"USER"				=> $row["user_id"],
											"DATE"				=> $row["changed_date"],
											"SUGGESTION"			=> $row["translated_string"],
											"VOTES"				=> $row["vote_points"],

											"ROW_TR_BGCOLOR"		=> $rowCount % 2 ? ROW_COLOR_1 : ROW_COLOR_2
											)
									);
					$rowCount++;
					$obj_tpl->parse("changes_ref", "changes", true);	
				}
			}
		}
		
		$obj_tpl->pparse("babel_single_dispute", "babel_single_dispute");
	break;
}

